//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Chart
{
    public abstract class ChartBase : ViewModelBase, IOfficeXmlSerializable
    {
        private ExtensionElement _extension;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; }
        }

       
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            IThemedElement e = ReadContextData.GetThemeOwner(context);
            if (e != null)
            {
                //\\e.ThemeChanged += delegate { Theme = e.Theme; };
                //\\ Theme = e.Theme;
            }
            ReadXmlCore(context);
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            VerifyAxes();
            WriteXmlCore(context);
        }

        protected abstract void VerifyAxes();

        #endregion

        internal abstract void ReadXmlCore(ReadContext context);

        internal abstract void WriteXmlCore(WriteContext context);

        internal ObservableCollection<T> CreateSeriesCollection<T>() where T : Series
        {
            ObservableCollection<T> result = new ObservableCollection<T>();
            result.CollectionChanged += delegate
                                            {
                                                int i = 0;
                                                foreach (Series s in result)
                                                {
                                                    s.Index = i;
                                                    s.Order = i++;
                                                }
                                            };
            return result;
        }


        internal abstract IEnumerable<Axis> GetAxes();
        internal void ReplaceCrossAxis(Axis oldAxis, Axis newAxis)
        {
            if (oldAxis == null)
                return;
            foreach (var axis in Enumerable.Where(GetAxes(), aa => aa != null))
            {
                if (axis.CrossAxis == oldAxis)
                    axis.CrossAxis = newAxis;
            }
        }

        internal void OnAxisChanged(Axis oldValue, Axis newValue)
        {
            if (oldValue == newValue)
                return;
            if (oldValue == null)
                return;
            foreach (var ax in Enumerable.Where(GetAxes(), a => a != null))
            {
                if (ax.CrossAxis == oldValue)
                    ax.CrossAxis = newValue;
            }
        }
    }
}